<?php
/**
 * Created by PhpStorm.
 * User: sun
 * Date: 18-3-15
 * Time: 下午8:53
 */

namespace App\Controller;

use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use App\Service\CellService;
use PhpOffice\PhpSpreadsheet\Style\Fill;

class DefaultController extends Controller
{
    private $wareHouse = [
        're' => [
            ['key' => ['发上海'], 'value' => '上海在途仓'],
            ['key' => ['发黄埔'], 'value' => '黄埔在途仓'],
            ['key' => ['东北发南浔'], 'value' => '东北-南浔在途刨光板仓'],
            ['key' => ['深圳观澜'], 'value' => '深圳观澜仓']
        ],
        'deng' => [
            ['key' => ['新湖州南浔', '新湖州南浔黑酸枝'], 'value' => '新湖州南浔仓'],
            ['key' => ['湖州南浔', '湖州南浔黑酸枝'], 'value' => '新湖州南浔仓'],
            ['key' => ['深圳观澜', '深圳观澜-楷源'], 'value' => '深圳观澜仓'],
            ['key' => ['远东万达仓'], 'value' => '远东万达仓'],
            ['key' => ['远东万达纵刨转仓'], 'value' => '远东万达纵刨转仓'],
            ['key' => ['远东万达框锯仓'], 'value' => '远东万达框锯仓'],
            ['key' => ['东北鼎源仓'], 'value' => '东北鼎源仓'],
        ]
    ];

    private $rate=['',"AA","A", "Z*AB", "AB", "B2", "C", "D", "CD", "B*AB", "B*B", "B*A", "D*AA", "D*AB", "GC", "LB", "B", "弯板", "ABC", "ABCD", "DA", "ZB", "A2", "Z*A", "三拼", "E", "四拼", "黑线", "双色", "Z*125AA", "Z*125A白", "Z*125AB", "Z*125黑线", "XJ", "背板", "狂放", "A红2", "D", "A白", "B", "A白", "AB弯板", "D", "B", "A白", "A色", "A红"];

    private $leixing = ['','刨光干板_地板材','刨光干板_拼板','水板_地板材','刨光干板_木皮','水板_家具材'];

    private $alpha = array('E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');


    /**
     * @Route("/")
     */
    public function index(Request $request)
    {
        $name = null;
        $span = 0;
        # 查找上传文件
        $results = [];
        $wareHouseMsg = [];
        $noWareHouseMsg = [];
        # 清空多余文件
        $name1 = null;
        $name2 = null;
        if ($request->isMethod('POST')) {
            $fileSystem = new Filesystem();
            $fileSystem->remove('output');
            $fileSystem->mkdir('output');
            $start_date = time();
            $file = $request->files->get('file');
            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
            $reader->setReadDataOnly(true);
            $spreadsheet = $reader->load($file->getRealPath());
            $worksheet = $spreadsheet->getSheet(0);
            $worksheet->getRowIterator(1, 200);
            foreach ($worksheet->getRowIterator(1) as $row) {
            }
            for ($i = 2; $i < $worksheet->getHighestRow() + 1; $i++) {
                $ATo = $i - 1;
                #树种 A
                $Acell = $worksheet->getCell('A' . $i);
                # 等级 B
                $Bcell = $worksheet->getCell('B' . $i);
                # 长度
                $Ccell = $worksheet->getCell('C' . $i);
                # 宽度
                $Dcell = $worksheet->getCell('D' . $i);
                # 厚度
                $Ecell = $worksheet->getCell('E' . $i);
                # 数量
                $Hcell = $worksheet->getCell('H' . $i);
                # 单位
                $Icell = $worksheet->getCell('I' . $i);
                # 仓库
                $Jcell = $worksheet->getCell('J' . $i);
                # 类型
                $Kcell = $worksheet->getCell('K' . $i);

                # 类型处理
                $Kvalue = $Kcell->getValue();
                $KArray = explode('_', $Kvalue);
                $BTo = '';
                if ($KArray[1] == '地板材') {
                    # 树种+类型_前部分+长*宽
                    $BTo = $Acell->getValue() . $KArray[0] . $Ccell->getValue() . '*' . $Dcell->getValue();
                } elseif ($KArray[1] == '木皮') {
                    $BTo = $Acell->getValue() . $KArray[1] . $Ccell->getValue() . '*' . $Dcell->getValue() . '*' . $Ecell->getValue();
                } else {
                    $BTo = $Acell->getValue() . $Kvalue;
                    if ($Ccell->getValue()) {
                        $BTo .= $Ccell->getValue();
                    }
                    if ($Ccell->getValue() && $Dcell->getValue()) {
                        $BTo .= '*' . $Dcell->getValue();
                    } else {
                        $BTo .= $Dcell->getValue();
                    }
                }
                # $CTo 一级分类
                $CTo = $KArray[1];
                # 二级分类 / 树种
                $DTo = ($Acell->getValue()) . '';

                # 等级 F
                $FTo = $Bcell->getValue() . '';
                # 长G，宽H，高I
                $GTo = $Ccell->getValue();
                $HTo = $Dcell->getValue();
                $ITo = $Ecell->getValue();
                # J单位 K库存
                $JTo = $Icell->getValue() . '';
                $KTo = $Hcell->getValue();

                # 处理仓库 $wareHouse $ETo
                $Jvalue = $Jcell->getValue();
                $ETo = $Jvalue;
                $row = [$BTo, $CTo, $DTo, $ETo, $FTo, $GTo, $HTo, $ITo, $JTo, $KTo];
                $results[] = $row;
            }
            # 冒泡去重
            # 带仓库
            $warehouseResults = [];
            # 不带仓库
            $noWareHouseResults = [];

            $rj = 1;
            foreach ($results as $re) {
                $rj++;
                # 记住索引
                $re[] = $rj;
                # 判断 0,-7，添加
                $isSame = false;
                foreach ($warehouseResults as $k => $wr) {
                    $isSame = static::validateArray($re, $wr, 0, 7);
                    if ($isSame) {
                        if (!array_key_exists($wr[10], $wareHouseMsg)) {
                            $wareHouseMsg[$wr[10]] = [];
                        }
                        $wareHouseMsg[$wr[10]][] = $rj;
                        $warehouseResults[$k][9] += $re[9];
                        break;
                    }
                }
                if (!$isSame) {
                    $warehouseResults[] = $re;
                }

                # 没有仓库的
                $isSameNoHouse = false;
                foreach ($noWareHouseResults as $k2 => $nwr) {
                    $isSameNoHouse = (static::validateArray($re, $nwr, 0, 2) && static::validateArray($re, $nwr, 4, 7));
                    if ($isSameNoHouse) {
                        if (!array_key_exists($nwr[10], $noWareHouseMsg)) {
                            $noWareHouseMsg[$nwr[10]] = [];
                        }
                        $noWareHouseMsg[$nwr[10]][] = $rj;
                        $noWareHouseResults[$k2][9] += $re[9];
                        break;
                    }
                }
                if (!$isSameNoHouse) {
                    $noWareHouseResults[] = $re;
                }
            }

            $spreadsheet->disconnectWorksheets();
            unset($spreadsheet);
            # 形成文件
            $spreadsheet2 = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet2->getActiveSheet();
            $sheet->setTitle('未合并仓库的数据');
            # 序号	商品品名	一级分类	二级分类	仓库	等级	长	宽	厚（高）	单位	真实库存
            $i = 2;
            $sheet->setCellValue('A1', '序号');
            $sheet->setCellValue('B1', '商品品名');
            $sheet->setCellValue('C1', '一级分类');
            $sheet->setCellValue('D1', '二级分类');
            $sheet->setCellValue('E1', '仓库');
            $sheet->setCellValue('F1', '等级');
            $sheet->setCellValue('G1', '长');
            $sheet->setCellValue('H1', '宽');
            $sheet->setCellValue('I1', '厚（高）');
            $sheet->setCellValue('J1', '单位');
            $sheet->setCellValue('K1', '真实库存');
            foreach ($warehouseResults as $ws) {
                $sheet->setCellValue('A' . $i, $i - 1);
                $sheet->setCellValue('B' . $i, $ws[0]);
                $sheet->setCellValue('C' . $i, $ws[1]);
                $sheet->setCellValue('D' . $i, $ws[2]);
                $sheet->setCellValue('E' . $i, $ws[3]);
                $sheet->setCellValue('F' . $i, $ws[4]);
                $sheet->setCellValue('G' . $i, $ws[5]);
                $sheet->setCellValue('H' . $i, $ws[6]);
                $sheet->setCellValue('I' . $i, $ws[7]);
                $sheet->setCellValue('J' . $i, $ws[8]);
                $sheet->setCellValue('K' . $i, $ws[9]);
                $i++;
            }
            # 没有仓库的

            $sheet2 = $spreadsheet2->createSheet();
            $sheet2->setTitle('合并仓库的数');
            # 序号	商品品名	一级分类	二级分类	仓库	等级	长	宽	厚（高）	单位	真实库存

            $i = 2;
            $sheet2->setCellValue('A1', '序号');
            $sheet2->setCellValue('B1', '商品品名');
            $sheet2->setCellValue('C1', '一级分类');
            $sheet2->setCellValue('D1', '二级分类');
            $sheet2->setCellValue('E1', '仓库');
            $sheet2->setCellValue('F1', '等级');
            $sheet2->setCellValue('G1', '长');
            $sheet2->setCellValue('H1', '宽');
            $sheet2->setCellValue('I1', '厚（高）');
            $sheet2->setCellValue('J1', '单位');
            $sheet2->setCellValue('K1', '真实库存');
            foreach ($noWareHouseResults as $ws) {
                $sheet2->setCellValue('A' . $i, $i - 1);
                $sheet2->setCellValue('B' . $i, $ws[0]);
                $sheet2->setCellValue('C' . $i, $ws[1]);
                $sheet2->setCellValue('D' . $i, $ws[2]);
                $sheet2->setCellValue('E' . $i, '');
                $sheet2->setCellValue('F' . $i, $ws[4]);
                $sheet2->setCellValue('G' . $i, $ws[5]);
                $sheet2->setCellValue('H' . $i, $ws[6]);
                $sheet2->setCellValue('I' . $i, $ws[7]);
                $sheet2->setCellValue('J' . $i, $ws[8]);
                $sheet2->setCellValue('K' . $i, $ws[9]);
                $i++;
            }

            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet2);
            $writer->setPreCalculateFormulas(false);
            $name1 = 'output/' . md5(uniqid()) . "1.xlsx";
            $writer->save($name1);
//            $name2 = 'output/'.md5(uniqid())."2.xlsx";
//            $writer->save($name2);
            # 不使用
            $spreadsheet2->disconnectWorksheets();
            unset($spreadsheet2);
            $end_date = time();
            # 删除临时文件
            $fileSystem->remove($file->getRealPath());
            $span = $end_date - $start_date;
        }

        return $this->render('default/index.html.twig', ['name' => $name, 'path1' => $name1, 'path2' => $name2, 'span' => $span, 'houseMsg' => $wareHouseMsg, 'nomsg' => $noWareHouseMsg]);
    }

    /**
     * @param $a
     * @param $b
     * @param int $start
     * @param int $end
     */
    public static function validateArray($a, $b, $start = 0, $end = 0)
    {
        if (count($a) < $end || count($b) < $end) {
            return false;
        }
        for ($i = $start; $i < $end + 1; $i++) {
            if ($a[$i] != $b[$i]) {
                return false;
            }
        }
        return true;
    }

    /**
     * 删除 output 文件
     */
    public static function deleteFiles()
    {
    }

    /**
     * @Route("/gather")
     */
    public function gather(Request $request, CellService $cellService)
    {
        $name = null;
        $span = 0;
        # 查找上传文件
        $results = [];
        $wareHouseMsg = [];
        $noWareHouseMsg = [];
        # 清空多余文件
        $name1 = null;
        $name2 = null;
        if ($request->isMethod('POST')) {
            $fileSystem = new Filesystem();
            $fileSystem->remove('gather');
            $fileSystem->mkdir('gather');
            $start_date = time();
            $file = $request->files->get('file');
            $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
            $reader->setReadDataOnly(true);
            $spreadsheet = $reader->load($file->getRealPath());
            $worksheet = $spreadsheet->getSheet(0);
            # 循环遍历数据
            for ($i = 2; $i < $worksheet->getHighestRow() + 1; $i++) {
                #树种 A
                $Acell = $worksheet->getCell('A' . $i);
                # 等级 B
                $Bcell = $worksheet->getCell('B' . $i);
                # 长度
                $Ccell = $worksheet->getCell('C' . $i);
                # 宽度
                $Dcell = $worksheet->getCell('D' . $i);
                # 厚度
                $Ecell = $worksheet->getCell('E' . $i);
                # 数量
                $Hcell = $worksheet->getCell('H' . $i);
                # 单位
                $Icell = $worksheet->getCell('I' . $i);
                # 仓库
                $Jcell = $worksheet->getCell('J' . $i);
                # 类型
                $Kcell = $worksheet->getCell('K' . $i);
                $ATo = $Acell->getValue();
                # 等级
                $BTo = $cellService->getRate($Bcell->getValue());
                $CTo = $Ccell->getValue();
                $DTo = $Dcell->getValue();
                $ETo = $Ecell->getValue();
                $HTo = $Hcell->getValue();
                $ITo = $Icell->getValue();
                $JTo = $Jcell->getValue();
                $KTo = $Kcell->getValue();
                $row = [$ATo, $BTo, $CTo, $DTo, $ETo, $HTo, $ITo, $JTo, $KTo];
                $results[] = $row;
            }

            # 合并一样的
            $big_map = [];
            # 仓库数
            $warehouses = [];
            foreach ($results as $re) {
                $key = md5($re[0] . $re[2] . "*" . $re[3] . "*" . $re[4] . $re[8]);
                # 建立基本结构
                if (!array_key_exists($key, $big_map)) {
                    $big_map[$key] = [
                        'base_value' => [$re[0], $re[2], $re[3], $re[4], $re[8], $re[6]],
                        'items' => [],
                        'b2' => $re[2] . "*" . $re[3] . "*" . $re[4]
                    ];
                }
                # 插入数据
                $items = $big_map[$key]['items'];
                # 检查规格
                if (!array_key_exists($re[1], $big_map[$key]['items'])) {
                    $big_map[$key]['items'][$re[1]] = [];
                }

                # 检查仓库
                if (!array_key_exists($re[7], $big_map[$key]['items'][$re[1]])) {
                    $big_map[$key]['items'][$re[1]][$re[7]] = 0;
                }
                # 组件仓库位置
                if (!array_key_exists($re[7], $warehouses)) {
                    $warehouses[$re[7]] = count($warehouses) + 1;
                }
                $cang = $big_map[$key]['items'][$re[1]][$re[7]];
                $big_map[$key]['items'][$re[1]][$re[7]] = $cang + $re[5];
            }

            # 排序
            uasort($big_map,function($a,$b){
                if($a['base_value'][0] == $b['base_value'][0]){
                    # 类型排序
                    $av = array_search($a['base_value'][4],$this->leixing);
                    $bv = array_search($b['base_value'][4],$this->leixing);
                    $av = ($av == false ? 100000:$av);
                    $bv = ($bv == false ? 100000:$bv);
                    if($av == $bv){
                        # 长，宽，高
                        if($a['base_value'][1] == $b['base_value'][1]){
                            if($a['base_value'][2] == $b['base_value'][2]){
                                if($a['base_value'][3] == $b['base_value'][3]){
                                    return 0;
                                }
                                return $a['base_value'][2] < $b['base_value'][2] ? 1:-1;
                            }
                            return $a['base_value'][2] < $b['base_value'][2] ? 1:-1;
                        }
                        return $a['base_value'][1] < $b['base_value'][1] ? 1:-1;
                    }
                    return $av > $bv? 1: -1;
                }
                return $a['base_value'][0] < $b['base_value'][0] ? 1:-1;
            });

            # 等级排序
            foreach ($big_map as $k =>$m){
                uksort($big_map[$k]['items'],function($a,$b){
                    $av = array_search($a,$this->rate);
                    $bv = array_search($b,$this->rate);
                    $av = ($av === false ? 100000:$av);
                    $bv = ($bv === false ? 100000:$bv);
                    if($av == $bv){
                        return 0;
                    }
                    return $av > $bv? 1 : -1;
                });
            }
            # 样式
            $allstyle = [
                'borders' => [
                    'allBorders' => [
                        'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                    ],
                ],
            ];
            # 第二行开始
            $style = ['borders' => [
                'allBorders' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                ],
            ],
                'fill' => [
                    'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR,
                    'rotation' => 0,
                    'startColor' => [
                        'rgb' => 'ddd9c4',
                    ],
                    'endColor' => [
                        'rgb' => 'ddd9c4',
                    ],
                ],
            ];
            $style2 = ['borders' => [
                'allBorders' => [
                    'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
                ],
            ],
                'fill' => [
                    'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_GRADIENT_LINEAR,
                    'rotation' => 0,
                    'startColor' => [
                        'rgb' => 'f2f2f2',
                    ],
                    'endColor' => [
                        'rgb' => 'f2f2f2',
                    ],
                ],
            ];

            # 写入EXCEL
            # 头部
            $spreadsheet2 = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
            $sheet = $spreadsheet2->getActiveSheet();
            $shuzhong = false;
            # 横向仓库变化
            $warehouse_b = [];
            $blueRow = [];
            $row = 1;
            foreach($big_map as $m){
                # 第一张表
                if($shuzhong === false){
                    $shuzhong = $m['base_value'][0];
                    $sheet->setTitle($shuzhong);
                }elseif($shuzhong != $m['base_value'][0]){
                    # 换表之前，更新
                    $style2['fill']['startColor']['rgb'] = 'f2f2f2';
                    $style2['fill']['endColor']['rgb'] = 'f2f2f2';
                    foreach ($blueRow as $b){
                        $sheet->getStyle('G' . $b['start'] . ':' . static::getAlpha(count($warehouse_b)-1) . $b['end'])
                            ->applyFromArray($style2);
                    }
                    $sheet->getStyle('A1:'.static::getAlpha(count($warehouse_b)-1).($row-1))->applyFromArray($allstyle);
                    #  换表,行数清0
                    $shuzhong = $m['base_value'][0];
                    $warehouse_b = [];
                    $blueRow = [];
                    $row = 1;
                    $sheet = $spreadsheet2->createSheet();
                    $sheet->setTitle($shuzhong);
                    $style2['fill']['startColor']['rgb'] = 'ffffff';
                    $style2['fill']['endColor']['rgb'] = 'ffffff';
                }
                if($row == 1){
                    # 设置表头
                    $sheet->setCellValue('A1', '树种');
                    $sheet->getColumnDimension('A')->setWidth(14);
                    $sheet->setCellValue('B1', '类型');
                    $sheet->getColumnDimension('B')->setWidth(18);
                    $sheet->setCellValue('C1', '规格');
                    $sheet->getColumnDimension('C')->setWidth(14);
                    $sheet->setCellValue('D1', '等级');
                    $sheet->setCellValue('E1', '小计');
                    $sheet->setCellValue('F1', '总计');
                    $sheet->setCellValue('G1', '单位');
                    $row++;
                }
                # 设置规格
                $total = 0.0;
                $rowtart = $row;
                foreach ($m['items'] as $k => $item) {
                    # 树种
                    $sheet->setCellValue('A' . $row, $m['base_value'][0]);
                    #类型
                    $sheet->setCellValue('B'. $row, $m['base_value'][4]);
                    #等级
                    $sheet->setCellValue('D' . $row, $k);
                    $small_total = 0.0;
                    # 各项数
                    foreach ($item as $kk => $vv) {
                        if(array_search($kk,$warehouse_b) === false){
                            $warehouse_b[] = $kk;
                            # 设置仓库表头
                            $sheet->setCellValue(static::getAlpha(count($warehouse_b) - 1) . '1', $kk);
                            $sheet->getColumnDimension(static::getAlpha(count($warehouse_b) - 1))->setWidth(mb_strwidth($kk)*1+2);
                        }
                        $key = array_search($kk, $warehouse_b);
                        $sheet->setCellValue(static::getAlpha($key) . $row, $vv);
                        $small_total += $vv;
                    }
                    # 小计
                    $sheet->setCellValue( 'E'. $row, $small_total);
                    $total += $small_total;
                    # 单位
                    $sheet->setCellValue('G'. $row, $m['base_value'][5]);
                    $mergeB[] = 'B' . $row;
                    $row++;
                }
                $rowEnd = ($row - 1);
                # 合并C
                $sheet->mergeCells('C' . $rowtart . ':C' . $rowEnd);
                $sheet->setCellValue('C' . $rowtart, $m['b2']);
                # 填颜色值，各个仓库
                #$sheet->getStyle('G' . $rowtart . ':' . static::getAlpha(count($warehouse_b)-1) . $rowEnd)
                #    ->applyFromArray($style2);
                $sheet->getStyle('C' . $rowtart . ':' . 'E' . $rowEnd)
                    ->applyFromArray($style2);
                if($style2['fill']['startColor']['rgb'] ==  'f2f2f2'){
                    #
                    $blueRow[] = ['start'=>$rowtart,'end'=>$rowEnd];
                    $style2['fill']['startColor']['rgb'] = 'ffffff';
                    $style2['fill']['endColor']['rgb'] = 'ffffff';
                }else{
                    $style2['fill']['startColor']['rgb'] = 'f2f2f2';
                    $style2['fill']['endColor']['rgb'] = 'f2f2f2';
                }
                # 合并总
                $sheet->getStyle('F' . $rowtart)->applyFromArray($style);
                $sheet->mergeCells('F' . $rowtart . ':' . 'F' . $rowEnd);
                $sheet->setCellValue('F'. $rowtart, $total);
            }
            $style2['fill']['startColor']['rgb'] = 'f2f2f2';
            $style2['fill']['endColor']['rgb'] = 'f2f2f2';
            foreach ($blueRow as $b){
                $sheet->getStyle('G' . $b['start'] . ':' . static::getAlpha(count($warehouse_b)-1) . $b['end'])
                    ->applyFromArray($style2);
            }
            $sheet->getStyle('A1:'.static::getAlpha(count($warehouse_b) -1).($row-1))->applyFromArray($allstyle);
            $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet2);
            $writer->setPreCalculateFormulas(false);
            $name1 = 'gather/' . md5(uniqid()) . "1.xlsx";
            $writer->save($name1);
            $spreadsheet2->disconnectWorksheets();
            unset($spreadsheet2);
            $end_date = time();
            # 删除临时文件
            $fileSystem->remove($file->getRealPath());
            $span = $end_date - $start_date;
        }

        return $this->render('default/gather.html.twig', ['name' => $name, 'path1' => '/' . $name1, 'path2' => $name2, 'span' => $span, 'houseMsg' => $wareHouseMsg, 'nomsg' => $noWareHouseMsg]);

    }

    private function sortMap($a,$b){
        # 名字
        if($a['base_value'][0] == $b['base_value'][0]){
            # 长，宽，高
            if($a['base_value'][1] == $b['base_value'][1]){
                if($a['base_value'][2] == $b['base_value'][2]){
                    if($a['base_value'][3] == $b['base_value'][3]){
                        return 0;
                    }
                    return $a['base_value'][2] > $b['base_value'][2] ? 1:-1;
                }
                return $a['base_value'][2] > $b['base_value'][2] ? 1:-1;
            }
            return $a['base_value'][1] > $b['base_value'][1] ? 1:-1;
        }
        return $a['base_value'][0] > $b['base_value'][0] ? 1:-1;
    }

    public static function getAlpha($index){
        $index = $index + 7;
        $alpha = ['A','B','C','D','E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

        $b = (int)($index/26);
        $pre = '';
        if($b != 0){
            $pre = $alpha[$b-1];
        }
        $v = $index % 26;
        return ($pre . $alpha[$v]);
    }

}